/****
 * 登录表单验证码
 * ****/


/***************************第一个表单验证码start**********************************/
document.getElementsByClassName('btn')[0].onclick = function () {
    var phone = document.getElementById('user1').value;
    var  password1=document.getElementById('password1').value;
    var password = hex_md5(password1);
    if(phone==''){
        alert('手机号或用户名不能为空！');
        $('#user1').focus();
        return false;
    }
    else if(password1==''){
        alert('密码不能为空！');
        $('#password1').focus();
        return false;
    }
    else{
        if(regBox.regMobile.test(phone)){
            var url = server + '/server/login/passwordLogin.json';
            var args = {"phone": phone, "password": password};
            $.post(url, args, function (data) {
                if (data.code >= 23100 && data.code <= 23102) {
                    window.location.replace('./index.html');
                }
                else {
                    alert("登录失败");
                    window.location.reload();
                }

            });
        }
        else{
            alert("手机号格式不正确");
        }
    }
};
/***************************第一个表单验证码end**********************************/

/***************************第二个表单验证码start**********************************/
document.getElementsByClassName('btn')[2].onclick = function () {
    var phone = document.getElementById('user').value;
    var code1 = document.getElementById('password').value;
    if(regBox.regMobile.test(phone)){
        code1 =  phone+code1;
        var md5 = hex_md5(code1);
        var url = server + '/server/login/captchaLogin.json';
        var url2 = server +'/server/login/password/isValid.json';

        var args = {"phone": phone,"md5": md5};
        $.post(url, args, function (data) {
            if (data.code >= 23100 && data.code <= 23102) {
                $.post(url2, function (data) {
                    if(data.code == 21300){
                        window.location.replace('./modify-password.html');
                    }
                    else{
                        window.location.replace('./index.html');
                    }
                });

            }
            else {
                alert("验证码错误");
            }

        });
    }
    else{
        alert("手机号格式不正确");
    }

};

/***************************第二个表单验证码end**************************************/

/***************************第二个表单获取验证码start*********************************/
var app = angular.module('myApp', []);
app.controller('myController', function ($scope,$interval) {
    $scope.user = '';
    $scope.email = '';
    $scope.password = '';
    $scope.canClick=false;//发送验证码按钮可以点击
    $scope.description='发送验证码';
    var second=59;
    var timerHandler;//定时器
    $scope.getTestCode=function(){
        var phone = document.getElementById('user').value;
        if(regBox.regMobile.test(phone)) {
            if (phone == '') {
                alert('请输入手机号');
                $('#user').focus();
                return false;
            }
            else {
                var url = server + '/server/login/request/captcha.json';
                var args = {"phone": phone};
                $.get(url, args, function (data) {
                    if (data.code == 11001) {
                        alert("手机号无效");
                        window.location.reload();
                    }
                });

                timerHandler = $interval(function () {
                    timer();
                }, 1000);
                function timer() {
                    if (second < 0) {
                        $interval.cancel(timerHandler);
                        second = 59;
                        $scope.canClick = false;
                        $scope.description = '发送验证码';
                    }
                    else {
                        $scope.description = second + '后重发';
                        second--;
                        $scope.canClick = true;//发送验证码按钮不可点击
                    }
                }
            }
        }
        else{
            alert("手机号格式不正确");
        }
    }
});
/***************************第二个表单获取验证码end**********************************/
